package com.kws.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.security.utils.SecurityUtils;
import com.kws.merchant.domain.MerchantSettledEntity;
import com.kws.merchant.mapper.MerchantSettledMapper;
import com.kws.merchant.service.MerchantSettledService;
import com.kws.merchant.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : zhangbenting
 * @date : 2024/10/30 20:20
 * @Version: 1.0
 */
@Service
public class MerchantSettledServiceImpl extends ServiceImpl<MerchantSettledMapper, MerchantSettledEntity>
        implements MerchantSettledService {

    @Value("${minio.ENDPOINT}")
    private String minioUrl;

    @Override
    public boolean merchantSettledApply(MerchantSettledEntity merchantSettled) {
        //实名认证
        String name = merchantSettled.getName();
        String idNumber = merchantSettled.getIdNumber();

        //实名通过后,添加入驻申请
        merchantSettled.setUserId(SecurityUtils.getUserId());
        return this.save(merchantSettled);
    }

    @Override
    public MerchantSettledEntity getSettledApplyInfo() {
        LambdaQueryWrapper<MerchantSettledEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MerchantSettledEntity::getUserId, SecurityUtils.getUserId());
        wrapper.eq(MerchantSettledEntity::getExamineStatus, "0");

        List<MerchantSettledEntity> list = this.list(wrapper);
        if (ObjectUtils.isNull(list)) return null;

        MerchantSettledEntity merchantSettledEntity = list.stream().findFirst().orElse(null);
        if (merchantSettledEntity == null) return null;

        String prefix = FileUtil.fixFileName();
        merchantSettledEntity.setIdFaceImg(prefix + merchantSettledEntity.getIdFaceImg());
        merchantSettledEntity.setIdNationalEmblemImg(prefix + merchantSettledEntity.getIdNationalEmblemImg());
        merchantSettledEntity.setBusinessLicenseImg(prefix + merchantSettledEntity.getBusinessLicenseImg());
        merchantSettledEntity.setQualificationCertificateImg(prefix + merchantSettledEntity.getQualificationCertificateImg());
        merchantSettledEntity.setApplyNumber(SecurityUtils.getUserId().toString());

        return merchantSettledEntity;
    }

    @Override
    public Long getSettledApplyCount() {
        LambdaQueryWrapper<MerchantSettledEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(MerchantSettledEntity::getUserId, SecurityUtils.getUserId());
        wrapper.eq(MerchantSettledEntity::getExamineStatus, "0");
        return this.count(wrapper);
    }


}
